package com.woniuxy.yogaapp.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.woniuxy.yogaapp.pojo.ClubImage;
import com.woniuxy.yogaapp.pojo.Coach;
import com.woniuxy.yogaapp.pojo.YogaClub;

public interface YogaClubMapper {
	// 查询所有会馆
	@Select("select * from yogaclub where approve=2")
	public List<YogaClub> findAllYogaClub();

	// 根据会馆id查询对应教练信息
	@Select("select * from yogaclub where yid=#{yid} and approve=2")
	@Results({ @Result(id = true, column = "yid", property = "yid"), @Result(column = "yname", property = "yname"),
			@Result(column = "yid", property = "coachs", many = @Many(select = "findCoach")) })
	public YogaClub findByCoach(int yid);


	@Select("select * from clubcoach cc , coach c where cc.cid=c.cid and cc.yid=#{yid} and cc.approve=2")
	public List<Coach> findCoach(int yid);

	// 根据会馆id查询对应教练id在查询出学员信息
	@Select("select * from yogaclub where yid=#{yid} and approve=2")
	@Results({ @Result(id = true, column = "yid", property = "yid"), @Result(column = "yname", property = "yname"),
			@Result(column = "yid", property = "coachs", many = @Many(select = "findCoachByYid")) })
	public YogaClub findByYid(int yid);


	
	@Select("select * from clubcoach cc , coach c where cc.cid=c.cid and cc.yid=#{yid}")
	@Results({ @Result(id = true, column = "cid", property = "cid"), @Result(column = "cname", property = "cname"),
			@Result(column = "cid", property = "students", many = @Many(select = "com.woniuxy.yogaapp.mapper.CoachMapper.findStudentsByCid")) })
	public List<Coach> findCoachByYid(int yid);



	// 认证场馆成功，approve状态为1
	@Update("update yogaclub set approve=2 where uid=#{uid}")
	int agreeYogaClub(int uid);

	// 认证场馆驳回，approve状态为2
	@Update("update yogaclub set approve=1 where uid=#{uid}")
	int disagreeYogaClub(int uid);

	// 根据yid找场馆
	@Select("select * from yogaclub where yid=#{yid} and approve=2")
	YogaClub findClubbyYid(int yid);

	// 为场馆赋予权限
	@Insert("insert into userroles (uid,rid) values (#{uid},4)")
	int setYogaClub(int uid);

	/**
	 * 根据用户id 查询该用户是否是新用户
	 * 
	 * @param yogaClub
	 * @return
	 */
	@Select("select *from yogaclub where uid=#{uid}")
	public YogaClub findYogaClubByUid(YogaClub yogaClub);

	/**
	 * 新用户 注册场馆 添加基本信息
	 * 
	 * @param yogaClub
	 * @return
	 */
	@Insert("insert into yogaclub(yname,yaddress,ytel,`describe`,yqq,uid,yaddrmes,ymail) "
			+ "values (#{yname},#{yaddress},#{ytel},#{describe},#{yqq},#{uid},#{yaddrmes},#{ymail})")
	public int addYogaClubInfo(YogaClub yogaClub);

	/**
	 * 老用户 修改场馆基本信息
	 * 
	 * @param yogaClub
	 * @return
	 */
	@Update("update yogaclub set yname=#{yname},ymail=#{ymail},yaddress=#{yaddress}"
			+ ",ytel=#{ytel},`describe`=#{describe},yqq=#{yqq},yaddrmes=#{yaddrmes} where uid=#{uid}")
	public int updateYogaClubInfo(YogaClub yogaClub);

	/**
	 * 用户发送认证请求 将认证信息保存到对应的详情信息
	 */
	@Update("update yogaclub set legalperson=#{legalperson},yidcard=#{yidcard} where uid=#{uid}")
	public int updateYogaClubApprove(YogaClub yogaClub);

	@Select("select * from clubimage where yid=#{yid}")
	List<ClubImage> findImagebyYid(int yid);

	// 根据uid找场馆
	@Select("select * from yogaclub where uid=#{uid} and approve=2")
	YogaClub findClubbyUid(Integer uid);

}
